 //
//  TSYBaseViewController.swift
//  TSYWeiBoDemo
//
//  Created by apple on 2016/11/23.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class TSYBaseViewController: UIViewController {
    
    var tableView:UITableView?
    
    var isPull:Bool = false
    
    var isLogin = true
    
    var ref:UIRefreshControl?
    
    var vcInfoDic:[String:String]?
    
    func loadDate() {
        print("上啦加载")
        print("下啦刷新")
    }
    
    var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
    
    lazy var navItem = UINavigationItem()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadDate()
        
        setupUI()
        // Do any additional setup after loading the view.
        
        NotificationCenter.default.addObserver(self, selector: #selector(successLogin), name: NSNotification.Name(rawValue:"登陆成功"), object: nil)
    }

    override var title: String? {
        didSet {
            navItem.title = title
        }
    }
    
    func successLogin(){
        
    }

}
 
 //MARK: 访客试图监听
 extension TSYBaseViewController{
    
    @objc private func login(){
        
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: notiLogin), object: nil)
        
    }
    
    
    
 }
 
 
 extension TSYTabbarViewController{
    
    ///设置新特性试图
    private func setupNewfeatureViews(){
        
        //1.取设备版本号
        
        print(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "")
        
        
    }
    
    
    ///判断是不是新版本
    private var isNewVersion:Bool{
        
        return false
    }
    
    
 }
 
 extension TSYBaseViewController{
    
    func setupUI(){
        view.backgroundColor = UIColor.red
        
        navigationBar.barTintColor = UIColor.red
        
        view.addSubview(navigationBar)
        
        navigationBar.items = [navItem]
        
        automaticallyAdjustsScrollViewInsets = false
        
        ref = UIRefreshControl()
        
        tableView?.addSubview(ref!)
        
        ref?.addTarget(self, action: #selector(loadDate), for:UIControlEvents.valueChanged)
        
        if isLogin {
            setupTableView()
        }else{
          setupVisitView()
        }
        
    }
    
    /// 访客试图
    func setupVisitView() {
        
        let vc = TSYVisitView(frame: view.bounds)
        vc.backgroundColor = UIColor.red
        
        view.insertSubview(vc, belowSubview: navigationBar)
        
        guard let diccc = vcInfoDic else{
            return
        }
        
        vc.visitorInfo? = diccc
    }
    
    private func setupTableView(){
        
        tableView = UITableView(frame: view.bounds, style: UITableViewStyle.plain)
        
        view.insertSubview(tableView!, belowSubview: navigationBar)
        
        tableView?.delegate = self
        
        tableView?.dataSource = self

        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
    }
}
 
extension TSYBaseViewController: UITableViewDelegate,UITableViewDataSource {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    
    
    /// 上啦加载
    ///
    /// - Parameters:
    ///   - tableView: view
    ///   - cell: cell
    ///   - indexPath: nil
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = indexPath.section
        
        if row<0||section<0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        if row == (count-1) && !isPull {
            print(" 上啦加载 ")
            
            isPull = true
        }
    }
}
 
 
 
 
 
 
 
 
